package Lono::Action::LoadFromWiki;

use strict;
use warnings;

use Apache::Constants qw( :common );
use Apache::Request;

use Socialtext::Resting;
use URI::Escape;

#use base qw( Lono::Action::Base );

use constant PAGE_TAG => 'testrecipe';
use constant WIKI_URL => 'http://www.socialtext.net/marclavallee/index.cgi?';

sub handler {
    my $r = Apache::Request->new( shift );
    my $config = _get_rest_config( $r );
         
    my $resting = Socialtext::Resting->new( %{$config} );
    $resting->workspace( $config->{'workspace'} );
    
    my @pages = $resting->get_taggedpages( PAGE_TAG ); 
   
    $r->send_http_header( "text/html" );
    
    my $wiki_url = _get_wiki_url( $config );
    $r->print( "<ul>" );
    foreach my $page (@pages) {
        $r->print( qq[<li><a href="] . $wiki_url . uri_escape( $page ) . qq[">$page</a></li>] );   
    }
    $r->print( "</ul>" );

    return OK;
}

sub _get_rest_config {
    my $r = shift;
    return {'username'  => $r->dir_config( "ST_USERNAME" ),
            'password'  => $r->dir_config( "ST_PASSWORD" ),
            'server'    => $r->dir_config( "ST_SERVER" ),
            'workspace' => $r->dir_config( "ST_WORKSPACE" )};
}

sub _get_wiki_url {
    my $config = shift;
    return $config->{'server'} . '/' . $config->{'workspace'} . '/index.cgi?';
}

1;
